﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ClusterMethod.cs" company="DissingNelleman">
//   
// All rights reserved. </copyright>
// <summary>
//   The cluster method.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ServiceLibrary.Services.Dto
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;

    /// <summary>
    /// The cluster method.
    /// </summary>
    [DataContract]
    public class ClusterMethod
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the archive location.
        /// </summary>
        [DataMember]
        public virtual string ArchiveLocation { get; set; }

        /// <summary>
        /// Gets or sets the comments.
        /// </summary>
        [DataMember]
        public virtual string Comments { get; set; }

        /// <summary>
        /// Gets or sets the list of files related to the cluster method
        /// </summary>
        [DataMember]
        public virtual List<Guid> Files { get; set; }

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        [DataMember]
        public Guid ID { get; set; }

        /// <summary>
        /// Gets or sets the type.
        /// </summary>
        [DataMember]
        public virtual Guid Type { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The convert.
        /// </summary>
        /// <param name="method">
        /// The method.
        /// </param>
        /// <returns>
        /// The <see cref="ClusterMethod"/>.
        /// </returns>
        public static ClusterMethod Convert(Models.Domain.ClusterMethod method)
        {
            if (method != null)
            {
                var files = new List<File>();
                if (method.Files != null)
                {
                    foreach (Models.Base.File file in method.Files)
                    {
                        files.Add(File.Convert(file));
                    }
                }

                var dto = new ClusterMethod
                              {
                                  ID = method.ID, 
                                  ArchiveLocation = method.ArchiveLocation, 
                                  Comments = method.Comments, 
                                  Files = files.Select(x => x.ID).ToList(), 
                                  Type = (method.Type != null ? method.Type.ID : Guid.Empty) 
                              };
                return dto;
            }
            
            return null;
        }

        #endregion
    }
}

namespace ServiceLibrary.Services.Queries
{
    using System.Runtime.Serialization;

    using NHibernate;

    using ServiceLibrary.Actioners.Activities;
    using ServiceLibrary.Services.Results;

    /// <summary>
    /// The cluster method query.
    /// </summary>
    [DataContract]
    public class ClusterMethodQuery : IQuery
    {
        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <returns>
        /// The <see cref="IResult"/>.
        /// </returns>
        public override IResult Execute(ISession hsession)
        {
            var actioner = new ClusterMethodActioner(hsession, null);

            return actioner.ExecuteSearchQuery(this);
        }

        #endregion
    }
}

namespace ServiceLibrary.Services.Results
{
    using System.Collections.Generic;
    using System.Runtime.Serialization;

    using ServiceLibrary.Services.Dto;

    /// <summary>
    /// The cluster method result.
    /// </summary>
    [DataContract]
    public class ClusterMethodResult : IResult
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ClusterMethodResult"/> class.
        /// </summary>
        public ClusterMethodResult()
        {
            this.List = new List<ClusterMethod>();
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the list.
        /// </summary>
        [DataMember]
        public List<ClusterMethod> List { get; set; }

        /// <summary>
        /// Gets or sets the total row count.
        /// </summary>
        [DataMember]
        public int TotalRowCount { get; set; }

        #endregion
    }
}

namespace ServiceLibrary.Services.Commands
{
    using System;
    using System.Runtime.Serialization;

    using NHibernate;

    using ServiceLibrary.Actioners.Activities;
    using ServiceLibrary.Services.Dto;

    using User = ServiceLibrary.Models.Domain.User;

    /// <summary>
    /// The create cluster method command.
    /// </summary>
    [DataContract]
    public class CreateClusterMethodCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the method.
        /// </summary>
        [DataMember]
        public ClusterMethod Method { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, User activeUser)
        {
            var actioner = new ClusterMethodActioner(hsession, activeUser);
            actioner.CreateClusterMethod(this);
        }

        #endregion
    }

    /// <summary>
    ///     Only add files to the dto if they are not already on the clustermethod.
    /// </summary>
    [DataContract]
    public class UpdateClusterMethodCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the dto.
        /// </summary>
        [DataMember]
        public ClusterMethod Dto { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, User activeUser)
        {
            var actioner = new ClusterMethodActioner(hsession, activeUser);
            actioner.UpdateClusterMethod(this);
        }

        #endregion
    }

    /// <summary>
    /// The delete cluster method command.
    /// </summary>
    [DataContract]
    public class DeleteClusterMethodCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the dto.
        /// </summary>
        [DataMember]
        public Guid? Dto { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, User activeUser)
        {
            var actioner = new ClusterMethodActioner(hsession, activeUser);
            actioner.DeleteClusterMethod(this);
        }

        #endregion
    }

    /// <summary>
    /// The delete cluster method command.
    /// </summary>
    [DataContract]
    public class DeleteClusterMethodFileCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the Owner Cluster Method ID.
        /// </summary>
        [DataMember]
        public Guid? OwnerClusterMethodID { get; set; }

        /// <summary>
        /// Gets or sets the Cluster Method File ID.
        /// </summary>
        [DataMember]
        public Guid? ClusterMethodFileID { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, User activeUser)
        {
            var actioner = new ClusterMethodActioner(hsession, activeUser);
            actioner.DeleteClusterMethodFile(this);
        }

        #endregion
    }
}